/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Model;
import java.util.*;
import java.sql.Timestamp;
import AirLine.*;
import java.io.Serializable;
/**
 *
 * @author oskar
 */
public class Trip implements Serializable{

    final int PRICE=TripHandler.SORT_BY_PRICE, TIME=TripHandler.SORT_BY_TIME;
    
    private LinkedList<Flight> flights;
    
    public Trip(LinkedList<Flight> flights)
    {
        this.flights=new LinkedList<Flight>(flights);
        
    }
    
    public void addFlight(Flight f)
    {
        flights.addLast(f);
        
    }
    
    public LinkedList<Flight> getFlights()
    {
        return flights;
    }
    
    public Airport getDepartureAirport()
    {
       // System.out.println(flights.getFirst().getDepartureAirport());
        return flights.getFirst().getDepartureAirport();
    }
    
        public Airport getDestinationAirport()
    {
        //System.out.println(flights.getFirst().getDestinationAirport());
        return flights.getLast().getDestinationAirport();
    }
    
    public long getCost(int sort)
    {
        //Calculate from all flights
       if(sort==PRICE)
           return getPrice();
       else
           return getDuration();
    }
    
    public int getPrice()
    {
        boolean allSame=true;
        String oneAirline=flights.getFirst().getAirline().getCode();
        int price=0;
         //Calculate from all flights
        for(Flight f: flights)
        {
            if(allSame)
            {
                if(!(f.getAirline().getCode().equalsIgnoreCase(oneAirline)))
                    allSame=false;
            }
            price+=f.getPrice();
            
        }
        if(allSame)          
            price=(int)(price*(1-flights.get(0).getAirline().getDiscount()*0.01));
        return price;
    }
    
    public long getDuration()
    {
        return (flights.getLast().getArrivalTime().getTime()-flights.getFirst().getDepartureTime().getTime());
    }
    
    public long getArrivalTime()
    {
        return flights.getLast().getArrivalTime().getTime();
    }
    
    /*
    public String toString()
    {
        String s="";
        for(Flight f: flights)
        {
            s=s+f.getCode()+"-";
        }
        return s;
    }
     * */
}
